//
//  TransitionView.m
//  AnimationDemo
//
//  Created by Jiaozl 2018 on 2018/9/11.
//  Copyright © 2018年 Jiaozl. All rights reserved.
//

#import "ZLTransitionView.h"

@interface ZLTransitionView() <CAAnimationDelegate>

@property (nonatomic, strong) NSMutableArray *animArr;

@property (nonatomic, assign) NSInteger index;

@end

@implementation ZLTransitionView

- (instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        self.animArr = [[NSMutableArray alloc] initWithCapacity:0];
        
        NSArray *aArr = @[@"cube", @"suckEffect", @"rippleEffect", @"pageCurl", @"pageUnCurl", @"oglFlip", @"cameraIrisHollowOpen", @"cameraIrisHollowClose", @"spewEffect",@"genieEffect",@"unGenieEffect",@"twist",@"tubey",@"swirl",@"charminUltra", @"zoomyIn", @"zoomyOut", @"oglApplicationSuspend"];
        
        [self.animArr addObjectsFromArray:aArr];
        
        self.index = 0;
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];
    
    [self addTransition];
}

- (void)addTransition {
    CATransition *transitionAni = [[CATransition alloc] init];
    transitionAni.delegate = self;
    transitionAni.duration = 3.0;
    transitionAni.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transitionAni.type = self.animArr[self.index];
    transitionAni.subtype = kCATransitionFromLeft;
    transitionAni.repeatCount = 1;
    [transitionAni setValue:@"transitionAnim" forKey:@"anim"];
    
    [self.layer addAnimation:transitionAni forKey:@"transitionAnim"];
    
    self.text = [NSString stringWithFormat:@"%02ld 动画效果:%@",(long)self.index, self.animArr[self.index]];
}

#pragma mark - CAAnimationDelegate

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (flag) {
        self.index = (++self.index) % self.animArr.count;
        [self addTransition];
    }
}


@end
